package com.hmdp.whd;

import java.util.Stack;

@SuppressWarnings("AliControlFlowStatementWithoutBraces")
class MinStack {
    // stack就是栈本体,min维护当前出栈的最小值
    // 因为栈中min只需要和前面的栈元素min值相比较,借鉴DP的思想
    Stack<Integer> stack;
    Stack<Integer> min;
    /** initialize your data structure here. */
    public MinStack() {
        stack = new Stack<>();
        min = new Stack<>();
    }

    public void push(int x) {
        if(!stack.isEmpty()){
            stack.push(x);
            if(x < min.peek()) min.push(x);
            else min.push(min.peek());
        }else{
            stack.push(x);
            min.push(x);
        }
    }

    public void pop() {
        stack.pop();
        min.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int min() {
        return min.peek();
    }
}